package demo4.controller;

/**
 * Controller 控制器，后面学习SpringMVC时会介绍到
 * 这里简单理解
 * 它负责和客户端(用户打交道)
 */
import demo4.mapper.IUserMapper;
import demo4.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;
@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        Scanner sc= new Scanner(System.in);
        System.out.println("请输入序号：");
        System.out.println("1:用户注册");
        System.out.println("2:用户登录");
        System.out.println("3:修改密码");
        System.out.println("4:用户列表");
        int num =sc.nextInt();
        switch(num){
            case 1:
                regUser();
                break;
            case 2:
                login();
                break;
            case 3:
                System.out.println("修改密码");
                break;
            case 4:
                userlist();
                break;
        }
    }
    //插入用户
    private void regUser(){
        UserInfo user = new UserInfo();
        System.out.println("欢迎注册");
        Scanner sc= new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(sc.nextLine());
        System.out.println("请输入密码：");
        user.setPassword(sc.nextLine());
        System.out.println("请输入昵称：");
        user.setNickname(sc.nextLine());
        System.out.println("请输入年龄：");
        user.setAge(sc.nextInt());
        System.out.println(user);
        int num=userMapper.insert(user);
        System.out.println(num>0?"注册成功":"注册失败");
    }

    //用户登录，比较username和password
    public void login(){
        System.out.println("欢迎登录！");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=sc.nextLine();
        System.out.println("请输入密码：");
        String password=sc.nextLine();
        String a=userMapper.login(username,password);
        System.out.println(a);
    }

    //用户列表
    public void userlist(){
        System.out.println("欢迎查看");
        List<UserInfo> list=userMapper.userlist();
        for(UserInfo user:list){
            System.out.println(user);
        }
    }

}
